﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CNotebook;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.ReportSource;
using CrystalDecisions.Shared;

namespace NotebookS2
{
    public partial class CRYSTAL_SO_TOP5_AGENT : Form
    {
        public CRYSTAL_SO_TOP5_AGENT()
        {
            InitializeComponent();
        }
        public string ConnectionString { get; set; }
        public DateTime FromDate { get; set; }
        public DateTime ToDate { get; set; }

        public string GetLayout()
        {
            ReportConfigManager oManager = new ReportConfigManager();
            const int _DOCUMENT_TYPE = 41;
            oManager.ConnectionString = this.ConnectionString;

            oManager.Open();

            string _FILE_NAME = oManager.GetReportFileName(_DOCUMENT_TYPE);

            oManager.Close();

            return _FILE_NAME;
        }

        public void ViewTop5Agents()
        {
            ReportDocument rptDoc = new ReportDocument();
            SqlConnection oConnection = new SqlConnection();
            SqlCommand oCommand = new SqlCommand();
            SqlDataAdapter oAdapter = new SqlDataAdapter();
            DataSet ds = new DataSet();

            string sReportFile = "";

            sReportFile = GetLayout();

            rptDoc.Load(sReportFile);

            oConnection.ConnectionString = this.ConnectionString;
            oConnection.Open();

            oCommand.Connection = oConnection;
            oCommand.CommandType = CommandType.StoredProcedure;
            oCommand.CommandText = "RPT_TOP5_AGENT";

            oCommand.Parameters.Add(new SqlParameter("@FROM", this.FromDate));
            oCommand.Parameters.Add(new SqlParameter("@TO", this.ToDate));
            oAdapter.SelectCommand = oCommand;
            oAdapter.Fill(ds);

            oConnection.Close();

            rptDoc.SetDataSource(ds.Tables[0]);
            rptDoc.Subreports[0].SetDataSource(ds.Tables[0]);

            ParameterField oFrom = new ParameterField();
            ParameterField oTo = new ParameterField();



            ParameterFields oFields = new ParameterFields();
            rptDoc.SetParameterValue("@FROM", this.FromDate);
            rptDoc.SetParameterValue("@TO", this.ToDate);

            CRViewer.ReportSource = rptDoc;
            CRViewer.Refresh();
        }


        private void CRYSTAL_SO_TOP5_AGENT_Load(object sender, EventArgs e)
        {
            ViewTop5Agents();
        }
    }
}
